package com.wan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.entity.Departments;
import com.wan.entity.Staff;
import com.wan.mapper.StaffMapper;
import com.wan.service.IDepartmentsService;
import com.wan.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wan.until.PageResult;
import com.wan.vo.StaffVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sf
 * @since 2025-01-11
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {
    @Autowired
    private IDepartmentsService departmentsService;
    @Override
    public PageResult<Staff> findByPage(Integer page, Integer pageSize) {
        //分页
        Page<Staff> page01 = this.page(new Page<>(page, pageSize));
        //返回分页结果
        return new PageResult<>(page01.getRecords(),page01.getTotal());
    }
    // 条件带分页查询
    @Override
    public PageResult<Staff> search(Integer page, Integer pageSize, StaffVO vo) {

        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getEname()), "ename", vo.getEname())
                .like(StrUtil.isNotBlank(vo.getEmail()), "addr", vo.getEmail())
                .eq(vo.getDepartmentId() != null && vo.getDepartmentId() != 0, "did", vo.getDepartmentId());

        Page<Staff> page1 = this.page(new Page<>(page, pageSize), wrapper);

        page1.getRecords().forEach(st -> {
            Departments dep = departmentsService.getById(st.getDepartmentId());
            st.setDepartmentName(dep.getDepartmentName());
        });

        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public boolean add(Staff staff) {
        return this.save(staff);
    }
}
